package com.itcast.common.config;

import com.itcast.common.properties.HttpParameterProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author itcast
 */
@Configuration
public class RestTemplateConfig {
    @Resource
    private HttpParameterProperties httpParameterProperties;

    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate(simpleClientHttpRequestFactory());
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
        factory.setReadTimeout(httpParameterProperties.getReadTimeout());
        factory.setConnectTimeout(httpParameterProperties.getConnectTimeout());
        return factory;
    }
}